package com.chatplus.application.service.wxmp.handler;

import com.chatplus.application.domain.entity.account.WechatUserEntity;
import com.chatplus.application.service.wxmp.WeChatMpEventKeyHandler;
import com.chatplus.application.service.wxmp.builder.TextBuilder;
import com.chatplus.application.service.wxmp.impl.WeChatMpService;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

/**
 * @author chj
 */
public abstract class AbstractHandler implements WxMpMessageHandler {
    protected WeChatMpService weChatMpService;

    protected AbstractHandler(WeChatMpService weChatMpService) {
        this.weChatMpService = weChatMpService;
    }

    protected WxMpXmlOutMessage handleByEventKey(String eventKey, WechatUserEntity weChatUser, WxMpXmlMessage wxMpXmlMessage) {
        WeChatMpEventKeyHandler weChatMpEventKeyHandler = this.weChatMpService.getHandler(eventKey);
        if (weChatMpEventKeyHandler == null) {
            return new TextBuilder().build("该二维码已过期或发生了其它错误，请重试",
                    wxMpXmlMessage,
                    weChatMpService);
        }
        return weChatMpEventKeyHandler.handle(wxMpXmlMessage, weChatUser);
    }
}
